## Replication for Appendix Figure 1
# In case of any questions, please email anselm.rink@gmail.com

rm(list=ls())
library(foreign)
library(psych)
library(reshape2)
setwd("")
data <- read.dta("")

## Build indexes

#Economic marginalization index -- four variables: q7_has_job q8_has_income q8_income_last_month_ln q9_chance_become_rich
data$q8_income_last_month_ln <- ifelse(data$q8_income_last_month>0,log(data$q8_income_last_month),0)
(economically_marginalized_index <- alpha(data[, c("q7_has_job", "q8_has_income", "q8_income_last_month_ln", "q9_chance_become_rich")]))
economically_marginalized_index <- economically_marginalized_index$scores
economically_marginalized_index <- max(economically_marginalized_index, na.rm=T)-economically_marginalized_index
summary(economically_marginalized_index)

#Political marginalization index -- three variables q10_plans_to_vote q11_govt_represents_interest q12_feels_on_loosing_side
(politically_marginalized_index <- alpha(data[, c("q10_plans_to_vote", "q11_govt_represents_interest", "q12_feels_on_loosing_side")]))
politically_marginalized_index <- politically_marginalized_index$scores
plot(politically_marginalized_index)
politically_marginalized_index <- max(politically_marginalized_index, na.rm=T)-politically_marginalized_index
summary(politically_marginalized_index)

#Troubled family relations index -- three variables: q19_rlts_mother q20_rlts_father q21_respect_friends_family
(troubled_family_index <- alpha(data[,c("q19_rlts_mother", "q20_rlts_father", "q21_respect_friends_family" )]))
troubled_family_index <- troubled_family_index$scores
summary(troubled_family_index)

#Exposure to violence -- two variables: q15_witnesses_interrel_violence q16_witnesses_viol_musl_govt
(violence_exposure_index <- alpha(data[,c("q15_witnesses_interrel_violence", "q16_witnesses_viol_musl_govt")]))
violence_exposure_index <- violence_exposure_index$scores
summary(violence_exposure_index)

#Religiosity -- two variables: q22_religion q23_frequents_house_worship
(religiosity_index <- alpha(data[,c("q22_religion", "q23_frequents_house_worship")]))
religiosity_index <- religiosity_index$scores
summary(religiosity_index)

#Destructive life events -- five variables:  q17_friend_family_died q18_lost_job q18_arrested q18_house_of_worship_raided q18_stopped_speaking_parents q18_love_problems q18_friend_left_country
(destructive_events_index <- alpha(data[,c("q17_friend_family_died", "q18_lost_job", "q18_arrested", "q18_house_of_worship_raided", "q18_stopped_speaking_parents", "q18_love_problems", "q18_friend_left_country")]))
destructive_events_index <- destructive_events_index$scores
summary(destructive_events_index)

data$economically_marginalized_index <- economically_marginalized_index
data$politically_marginalized_index <- politically_marginalized_index
data$troubled_family_index <- troubled_family_index
data$violence_exposure_index <- violence_exposure_index
data$religiosity_index <- religiosity_index
data$destructive_events_index <- destructive_events_index


## Plot correlation matrix of indexes
library(ggplot2)

(M <- cor(data[,c("economically_marginalized_index", "politically_marginalized_index", "troubled_family_index", "violence_exposure_index", "religiosity_index", "destructive_events_index")], use="pairwise.complete.obs"))

colnames(M) <- rownames(M) <- c("Economic marginalization", "Political marginalization", "Troubled family", " Violence exposure", " Religiosity", "Destructive events")
p <- qplot(x=Var1, y=Var2, data=melt(cor(M, use="complete.obs")), fill=value, geom="tile")
p <- p + xlab("") + ylab("") +  scale_fill_gradient2(limits=c(-1, 1))
p
ggsave("cor.plot.pdf", width= 20, height = 6)

